package com.example.wujinapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wujinapi.dao.AddressDao;
import com.example.wujinapi.dao.FavouritesDao;
import com.example.wujinapi.entity.Address;
import com.example.wujinapi.entity.Favourites;
import com.example.wujinapi.entity.Users;
import com.example.wujinapi.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (Address)表服务实现类
 *
 * @author makejava
 * @since 2023-07-10 22:37:43
 */
@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, Address> implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public void addAddress(Address address) {
        Address adrData=addressDao.selectOne(new QueryWrapper<Address>()
                .eq("adrname", address.getAdrname())
                .eq("adrphone",address.getAdrphone())
                .eq("address",address.getAddress())
        );
        if(adrData==null){ //为空才插入新用户
            addressDao.insert(address);
        }
    }

    @Override
    public void updateAddressById(Address address) {
        Address datas=addressDao.selectOne(new QueryWrapper<Address>().eq("id", address.getId()));
        if(datas!=null){
            address.setId(datas.getId());
            addressDao.updateById(address);
        }
    }

    @Override
    public void deleteAddress(int addrId) {
        addressDao.delete(new QueryWrapper<Address>().eq("id",addrId));
    }

    @Override
    public List<Address> getAddressesByOpenid(String wxOpenid) {
        List<Address> Datas=addressDao.selectList(new QueryWrapper<Address>().eq("openid",wxOpenid));
        return Datas;
    }

    @Override
    public List<Map<String, Object>> getFavouritAddresses(int pageNum, int pageSize) {
        Page<Address> page = new Page<Address> (pageNum,pageSize); //实现定义的接口
        List<Map<String,Object>> Datas=addressDao.selectPageVo(page);
        return Datas;
    }
}

